﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RailwayReservationWeb.manager
{
    public partial class user_edit : System.Web.UI.Page
    {
        private RailwayReservationDataContext db;

        protected void Page_Load(object sender, EventArgs e)
        {
            db = new RailwayReservationDataContext();
            pMessage.Visible = false;
            txtUserName.ReadOnly = true;
            if (!IsPostBack)
            {
                if (!String.IsNullOrWhiteSpace(Request.QueryString["id"]))
                {
                    int id;
                    var index = Request.QueryString["id"];
                    if (!Int32.TryParse(index, out id))
                    {
                        Response.Redirect("~/manager/user_list.aspx");
                    }
                    if (id == CurrentAccountId())
                    {
                        Response.Redirect("~/manager/user_list.aspx");
                    }
                    var ob = db.Accounts.First(p => p.AccountId.Equals(id));
                    if (ob != null)
                    {
                        txtAccountId.Value = ob.AccountId.ToString();
                        txtUserName.Text = ob.UserName;
                        txtEmail.Text = ob.Email;
                        txtFullName.Text = ob.FullName;
                        txtSSN.Text = ob.SSN;
                        txtDob.Text = ob.DoB.ToString("MM/dd/yyyy");
                        cboGender.SelectedValue = ob.Gender.ToString();
                        txtAddress.Text = ob.Address;
                        txtPhone.Text = ob.Phone;
                        cboRole.SelectedValue = ob.Role.ToString();
                    }
                }
                else
                {
                    Response.Redirect("~/manager/User_list.aspx");
                }
            }

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (Page.IsValid)
                {
                    var ob = db.Accounts.First(p => p.AccountId.Equals(Int32.Parse(txtAccountId.Value)));
                    if (ob == null || ob.AccountId == CurrentAccountId())
                    {
                        Response.Redirect("~/manager/user_list.aspx");
                    }
                    if (ob != null)
                    {
                        ob.Email = txtEmail.Text.Trim();
                        ob.FullName = txtFullName.Text.Trim();
                        ob.SSN = txtSSN.Text.Trim();
                        ob.DoB = DateTime.Parse(txtDob.Text.Trim());
                        ob.Gender = Int16.Parse(cboGender.SelectedValue);
                        ob.Address = txtAddress.Text.Trim();
                        ob.Phone = txtPhone.Text.Trim();
                        ob.Role = Int16.Parse(cboRole.SelectedValue);
                    }
                    db.SubmitChanges();
                    Response.Redirect("~/manager/user_list.aspx");
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        private void ShowMessage(string msg)
        {
            pMessage.Visible = true;
            pMessage.InnerText = msg;
        }

        protected int CurrentAccountId()
        {
            var authinfo = (Account)Session["authinfo"];
            return authinfo.AccountId;
        }
    }
}
